package com.bio.entity;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import org.apache.commons.beanutils.BeanUtils;

@MappedSuperclass
public class BaseObject implements Serializable {

    private static final long serialVersionUID = 8257665517139109414L;
    
    public static final Integer FLAG_DELETED = 1;
    
    public static final Integer FLAG_UNDELETED = 0;
    
    public static final String CREATED_DATE = "createdDate";
    
    private Long id;

    private String morphiaId;
    
    private Calendar createdDate = Calendar.getInstance();
    
    private Long creator;
    
    private Calendar updatedDate;
    
    private Long updater;
    
    private Calendar deletedDate;
    
    private Long deleter;
    
    private Integer isDeleted = FLAG_UNDELETED;
    
    public boolean equals(Object obj) {
        if (obj == null || (obj.getClass() != this.getClass())) {
            return false;
        }
        
        BaseObject bo = (BaseObject) obj;
        if (bo.id == null && this.id == null) {
            return this.hashCode() == bo.hashCode();
        } else if (this.id != null && bo.id != null) {
            return this.id.equals(bo.id);
        } else {
            return false;
        }
        
    }
    
    public int hashCode() {
        if (this.id == null) {
            return super.hashCode();
        }
        
        return this.id.hashCode();
    }

    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name="created_date")
    public Calendar getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Calendar createdDate) {
        this.createdDate = createdDate;
    }

    @Column(name="creator")
    public Long getCreator() {
        return creator;
    }

    public void setCreator(Long creator) {
        this.creator = creator;
    }

    @Column(name="updated_date")
    public Calendar getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(Calendar updatedDate) {
        this.updatedDate = updatedDate;
    }

    @Column(name="updater")
    public Long getUpdater() {
        return updater;
    }

    public void setUpdater(Long updater) {
        this.updater = updater;
    }

    @Column(name="deleted_date")
    public Calendar getDeletedDate() {
        return deletedDate;
    }

    public void setDeletedDate(Calendar deletedDate) {
        this.deletedDate = deletedDate;
    }

    @Column(name="deleter")
    public Long getDeleter() {
        return deleter;
    }

    public void setDeleter(Long deleter) {
        this.deleter = deleter;
    }

    @Column(name="is_deleted")
    public Integer isDeleted() {
        return isDeleted;
    }

    public void setDeleted(Integer isDeleted) {
        this.isDeleted = isDeleted;
    }
    
    public void describe() {
        try {
            System.out.println(BeanUtils.describe(this).toString());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}

